<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form
        :model="queryParams"
        @submit.native.prevent
        ref="queryForm"
        size="small"
        :inline="true"
        class="clearfix"
        label-width="auto"
      >
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目类型" prop="projectTypeList">
          <el-select
            v-model="queryParams.projectTypeList"
            collapse-tags
            multiple
            placeholder="请选择项目类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.project_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目级别" prop="projectLevelList">
          <el-select
            v-model="queryParams.projectLevelList"
            multiple
            collapse-tags
            placeholder="请选择项目级别"
            clearable
          >
            <el-option
              v-for="dict in dict.type.project_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value * 1"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目进度" prop="projectProgressList">
          <el-select
            v-model="queryParams.projectProgressList"
            multiple
            collapse-tags
            placeholder="请选择项目进度"
            clearable
          >
            <el-option
              v-for="dict in dict.type.project_progress"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value * 1"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left: 0; margin-right: 0; margin-bottom: 0">
          <el-form-item label="统计时段" style="margin-bottom: 0px; margin-right: 5px" prop="dateType">
            <el-radio-group v-model="queryParams.dateType">
              <el-radio-button label="1">项目日期</el-radio-button>
              <el-radio-button label="2">中标日期</el-radio-button>
              <el-radio-button label="3">签约日期</el-radio-button>
            </el-radio-group></el-form-item
          >
          <el-form-item label="" style="margin-bottom: 0px; margin-left;: 5px;margin-right: 5px" prop="type">
            <el-radio-group v-model="queryParams.type" @change="changeTime">
              <el-radio-button label="month">本月</el-radio-button>
              <el-radio-button label="year">今年</el-radio-button>
            </el-radio-group></el-form-item
          >
          <el-form-item prop="time">
            <el-date-picker
              style="width: 240px"
              v-model="queryParams.time"
              type="daterange"
              range-separator="至"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" size="mini" plain @click="handleExport" icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-table
        v-loading="loading"
        :data="list"
        :header-cell-style="rowClass"
        :default-sort="{ order: 'descending' }"
        @sort-change="sortChange"
      >
        <!-- <template slot-scope="scope">
              <span>{{ parseTime(scope.row.expansionTime, '{y}-{m}-{d}') }}</span>
            </template> -->
        <el-table-column label="项目名称" width="180" align="center" prop="projectName"> </el-table-column>
        <el-table-column label="项目类型" align="center" prop="projectType"> </el-table-column>
        <el-table-column label="项目级别" width="120" sortable="custom" align="center" prop="projectLevel"> </el-table-column>
        <el-table-column label="项目进度" align="center" prop="projectProgress"> </el-table-column>
        <el-table-column label="项目金额（万元）" align="center" prop="projectAmount"> </el-table-column>
        <el-table-column label="签约额（万元）" align="center" width="130" prop="contractAmount"> </el-table-column>
        <el-table-column label="待回款（万元）" align="center" width="130" prop="pendingPayment"> </el-table-column>
        <el-table-column label="到期未开票（万元）" width="110" align="center" prop="expiredNotInvoiced">
        </el-table-column>
        <el-table-column label="已开票未回款（万元）" width="130" align="center" prop="invoicedUnpaid">
        </el-table-column>
        <el-table-column label="已回款（万元）" align="center" width="130" prop="receivedPayment"> </el-table-column>
        <el-table-column label="未到期（万元）" align="center" width="130" prop="unexpired"> </el-table-column>

        <el-table-column label="关联委外合同（份）" align="center" width="130" prop="outsourcingContractNum">
        </el-table-column>
        <el-table-column label="委外金额（万元）" align="center" prop="outsourcingContractAmount"> </el-table-column>
        <el-table-column label="应付款（万元）" width="130" align="center" prop="outsourcingContractAccountsPayable">
        </el-table-column>
        <el-table-column label="已付款（万元）" width="130" align="center" prop="outsourcingContractPaid">
        </el-table-column>
        <el-table-column label="未到期（万元）" align="center" width="130" prop="outsourcingContractUnexpired">
        </el-table-column>
        <el-table-column label="市场拓展支出（万元）" width="130" align="center" prop="marketExpansionExpenses">
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { projectDetailReport } from '@/api/biz/index'
import dayjs from 'dayjs'
export default {
  name: 'projectSummary',
  dicts: ['project_type', 'project_level', 'project_progress'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 市场拓展表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
        projectName: '',
        projectType: '',
        projectTypeList: [],
        projectLevel: '',
        projectLevelList: [],
        projectProgress: '',
        projectProgressList: [],
        dateType: '1',
        type: 'month',
      },
      expansionId: null,
      showDetails: false,
      typeList: [],
      disableds: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    sortChange({ order, prop }) {
      const type = {
        projectLevel: {
          ascending: 'project_level ASC',
          descending: 'project_level DESC',
        },
      }
      this.$set(this.queryParams, 'sortList', type[prop] && type[prop][order] ? [type[prop][order]] : [])
      this.$nextTick(() => {
        this.getList()
      })
    },
    changeTime() {
      const { type, time } = this.queryParams
      if (type == 'year') {
        this.queryParams.time = [dayjs().startOf('year').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      } else {
        this.queryParams.time = [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      }
    },
    rowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        if (columnIndex <= 10 && columnIndex > 4) {
          return { background: '#feffce  !important' }
        } else if (columnIndex > 9) {
          return { background: '#cfffff  !important' }
        }
      }
      return null
    },
    /** 查询列表 */
    getList() {
      const currentData = this.getParams()
      const time = new Date().getTime()
      const d = 300
      this.loading = true
      projectDetailReport(currentData)
        .then((response) => {
          const currentTime = new Date().getTime()
          const changeTime = currentTime - time > d ? 0 : d - (currentTime - time)
          const t = setTimeout(() => {
            clearTimeout(t)
            this.list = response.data
            // this.total = response.total
            this.loading = false
          }, changeTime)
        })
        .catch(() => {
          this.loading = false
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    getParams() {
      const currentData = JSON.parse(JSON.stringify(this.queryParams))
      const { time, contrast, timeContrast } = currentData
      if (time && time.length > 0) {
        currentData.beginDate = time[0]
        currentData.endDate = time[1]
      }
      if (contrast && timeContrast && timeContrast.length > 0) {
        currentData.beginDateContrast = timeContrast[0]
        currentData.endDateContrast = timeContrast[1]
      }
      if (currentData.type == 'month' || currentData.type == 'year') {
        delete currentData.type
      }
      delete currentData.time
      delete currentData.contrast
      delete currentData.timeContrast
      return currentData
    },
    /** 导出按钮操作 */
    handleExport() {
      const currentData = this.getParams()
      this.download(
        '/biz/statistics/project/projectDetailReportExport',
        currentData,
        `项目明细报表导出_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
